<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

/**
 * Plugin Library
 *
 * Registers and calls all plugins.  Any function below ***HOOKS*** is a valid
 * plugin function.
 */

class Plugin {
	
	var $registered;	//References to all registered plugins

	function Plugin()
	{
		$CI =& get_instance();
		
		$this->registered = array();
		
		$CI->lang->load('admin');
		log_message('debug', 'Plugin class initialized.');
	}
	
	// --------------------------------------------------------------------
	
	function register()
	{
		//Registers all plugins
		$CI =& get_instance();
		
		if ($handle = opendir(APPPATH.'plugins'))
		{
			while(false !== ($file = readdir($handle)))
			{
				if ($file[0] != '.' && substr($file, -3) == 'php')
				{
					include(APPPATH.'plugins/'.$file);
					$class = substr($file, 0, -4);
					
					$CI->db->where('name', $class);
					$query = $CI->db->get('plugins');
					if ($query->num_rows() < 1)
					{
						$tmp =& new $class();
						$CI->db->insert('plugins', array(
							'name' => $class,
							'enabled' => 0,
							'description' => $tmp->description
						));
						$this->install(&$tmp);
					}
					else
					{
						$tmp = $query->row_array();
						if ($tmp['enabled'] == TRUE)
						{
							$tmp = $this->registered[] =& new $class();
							$this->initialize($tmp);
						}
					}
				}
			}
			closedir($handle);		
		}
	}
	
	// --------------------------------------------------------------------
	
	// Convenience Function
	function get_plugin($name) {
		foreach($this->registered as $plugin) {
			if (strtolower(get_class($plugin)) == strtolower($name))
				return $plugin;
		}
		return FALSE;
	}
	
	// --------------------------------------------------------------------
	
	// Essentially a constructor, but does not get called for install
	/*
	| Use this is you need constructor arguments that require the installer
	| to have run.  i.e. Constructor database calls.
	*/
	function initialize($class)
	{
		if (method_exists($class, 'initialize'))
		{
			call_user_func(array($class, 'initialize'));
		}
	}
	
	// --------------------------------------------------------------------
	
	// Called when first installed
	function install($class)
	{
		if (method_exists($class, 'install'))
		{
			call_user_func(array($class, 'install'));
		}
	}
	
	// --------------------------------------------------------------------
	
	// Uninstaller
	function remove($class)
	{
		if (method_exists($class, 'remove'))
		{
			call_user_func(array($class, 'remove'));
		}
	}
	
	// --------------------------------------------------------------------
	
	// Properties page - included in admin panel
	function configure($class)
	{
		if (method_exists($class, 'configure'))
		{
			call_user_func(array($class, 'configure'));
		}
		else
		{
			$CI =& get_instance();
			$CI->session->set_flashdata('msg', $CI->lang->line('plugin_cannot_configure'));
			redirect('admin/plugins');
		}
	}
	
	// --------------------------------------------------------------------
	// --------------------------------------------------------------------
	
	/*** HOOKS ***/
	
	// Hooked to data before it's added to the edit box
	function pre_edit($text)
	{
		$return = $text;
		foreach($this->registered as $plugin)
		{
			if (method_exists($plugin, 'pre_edit'))
			{
				$return = call_user_func(array($plugin, 'pre_edit'), $text);
			}
			return $return;
		}
	}
	
	// --------------------------------------------------------------------
	
	// Hooked as post validation callback
	function post_submit($text)
	{
		$return = array(TRUE, $text);
		
		foreach($this->registered as $plugin)
		{
			if (method_exists($plugin, 'post_submit'))
			{
				$return = call_user_func(array($plugin, 'post_submit'), $return[1]);
				
				if ($return[0] === FALSE)
					return $return;
			}
		}
		return $return;
	}
	
	// --------------------------------------------------------------------

	// Hooked before login submit button
	function login_form()
	{
		$out = '';
		foreach($this->registered as $plugin)
		{
			if (method_exists($plugin, 'login_form'))
			{
				$out .= call_user_func(array($plugin, 'login_form'));
			}
		}
		return $out;
	}
	
	// --------------------------------------------------------------------
	
	// Hooked as login validation callback
	function process_login()
	{
		$return = array(TRUE, 'placeholder - irrelevant when true');
		
		foreach($this->registered as $plugin)
		{
			if (method_exists($plugin, 'process_login'))
			{
				$return = call_user_func(array($plugin, 'process_login'), $return[1]);
				
				if ($return[0] === FALSE)
					return $return;
			}
		}
		return $return;
	}
	
	// --------------------------------------------------------------------

	// Hooked before registration submit button
	function register_form()
	{
		$out = '';
		foreach($this->registered as $plugin)
		{
			if (method_exists($plugin, 'register_form'))
			{
				$out .= call_user_func(array($plugin, 'register_form'));
			}
		}
		return $out;
	}
	
	// --------------------------------------------------------------------

	// Hooked as registration validation callback
	function process_register()
	{
		$return = array(TRUE, 'placeholder - irrelevant when true');
		
		foreach($this->registered as $plugin)
		{
			if (method_exists($plugin, 'process_register'))
			{
				$return = call_user_func(array($plugin, 'process_register'), $return[1]);
				
				if ($return[0] === FALSE)
					return $return;
			}
		}
		return $return;
	}
}